<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%@page pageEncoding="UTF-8"%>
<%@include file="../WEB-INF/jspf/controlesessao.jspf"%>

<c:set var="id_turma" value="${param.id_turma}"/>
<c:set var="id_modulo" value="${param.id_modulo}"/>
<c:set var="id_turno" value="${param.id_turno}"/>

<sql:query var="reservas" dataSource="jdbc/dbo_MicrosoftSQLServer">
    select * from reservas
    inner join turmas on reservas.id_turma=turmas.id_turma
    where turmas.id_turma=?
    order by ordem
    <sql:param value="${param.id_turma}"/>
</sql:query>

<c:forEach var="reserva" items="${reservas.rows}">
    <!-- pegando o nome do parametro-->
    <c:set value="matricula_${reserva.id_componente}" var="param_matricula"/>

    <c:set var="matricula" value="${param[param_matricula]}"/>


    <div id="resultado_componente_${id_componente}">
        <c:choose>
            <c:when test="${empty matricula}">
                <p style="font-size: 12pt; color: red">Informe a matrícula do docente.</p>
            </c:when>
            <c:otherwise>

                <c:catch var ="catchException">
                    <sql:update var="query" dataSource="jdbc/dbo_MicrosoftSQLServer">
                        EXEC sp_gerar_alocacao_temporaria
                        ?
                        ,?
                        ,?
                        ,?
                        ,?
                        ,?
                        ,?
                        ,?
                        <sql:param value="${reserva.data_inicio}"/>
                        <sql:param value="${reserva.data_fim}"/>
                        <sql:param value="${matricula}"/>
                        <sql:param value="${reserva.id_turma}"/>
                        <sql:param value="${reserva.id_modulo}"/>
                        <sql:param value="${reserva.id_componente}"/>
                        <sql:param value="${reserva.id_sala}"/>
                        <sql:param value="${reserva.id_turno}"/>
                    </sql:update>

                    <sql:query var="query" dataSource="jdbc/dbo_MicrosoftSQLServer">
                        select a.*, c.desc_componente from Alocacoes_temp a
                        inner join componentes c on a.id_componente=c.id_componente
                        where situacao<>'Sem alocação' and data_inicio between ? and dateadd(day,1,?)
                        and matricula=?
                        and id_modulo=?
                        and a.id_componente=?
                        and a.id_sala=?
                        and a.id_turno=?
                        and a.id_turma=?
                        <sql:dateParam value="${reserva.data_inicio}"/>
                        <sql:dateParam value="${reserva.data_fim}"/>
                        <sql:param value="${matricula}"/>
                        <sql:param value="${reserva.id_modulo}"/>
                        <sql:param value="${reserva.id_componente}"/>
                        <sql:param value="${reserva.id_sala}"/>
                        <sql:param value="${reserva.id_turno}"/>
                        <sql:param value="${reserva.id_turma}"/>
                    </sql:query>


                    <c:choose>
                        <c:when test="${!empty query.rows}">
                            <br>
                            <div style="color:fuchsia"> Foram encontrados ${query.rowCount} conflitos </div>
                            <table class="tablescroll" id="tab_aloc" cellspacing="0" width="100%">
                                <thead>
                                    <th width="10%">Data</th>
                                    <th width="20%">Horário</th>
                                    <th width="70%">Situação</th>
                                </thead>
                                <tbody>
                                    <c:forEach var="row" items="${query.rows}">
                                        <fmt:formatDate var="data" value="${row.data_inicio}" pattern="dd/MM/yy"/>
                                        <fmt:formatDate var="horaI" value="${row.data_inicio}" pattern="HH:mm"/>
                                        <fmt:formatDate var="horaF" value="${row.data_fim}" pattern="HH:mm"/>
                                        <c:choose>
                                            <c:when test="${row.situacao!='Sem alocação'}">
                                                <c:set var="classe" value="aviso"/>
                                            </c:when>
                                            <c:otherwise>
                                                <c:set var="classe" value="normal"/>
                                            </c:otherwise>
                                        </c:choose>

                                        <tr>
                                            <td>${data}</td>
                                            <td>${horaI} - ${horaF}</td>
                                            <td class="${classe}">${row.situacao}</td>
                                        </tr>
                                    </c:forEach>
                                </tbody>
                            </table>
                        </c:when>
                        <c:otherwise>
                            <p style="font-size: 12pt; color: blue">Nenhum conflito foi encontrado.</p>
                        </c:otherwise>
                    </c:choose>

                </c:catch>
                <c:if test = "${catchException!=null}">
                    <br>
                    Erro : Não foi possível gerar a alocação.<br>
                    <p  style="color:red">${fn:substringAfter(catchException,"BD:")}</p>
                    ${catchException}
                </c:if>
            </c:otherwise>
        </c:choose>
    </div>
    <script>

    </script>

</c:forEach>



